package com.zjitc.p2.obj;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/13 16:57
 * @description:
 */
public class AProxy implements InvocationHandler{
  private M target;
  public Object getProxy(M target){
    this.target =target;
    Class clazz=target.getClass();
    return Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if(method.getName()=="A"){
      System.out.println("init");
      method.invoke(target,args);
      System.out.println("destroy");
    }else {
      method.invoke(target,args);
    }
    return null;
  }
}
